<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2023/8/2
 * Time: 10:43
 */

namespace app\admin\model;
use think\Config;
use think\Db;
use think\Log;
use think\Model;

class Lianlians extends Model
{
    /**
     * 分类
     */
    public function getCategoryList(){
        $route = 'https://channel-api.lianlianlvyou.com/ll/channel/product/getCategoryList';
        $params = json_encode([
            'isTree'=>0,
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    /**
     * 订单查询
     */
    public function getOrderInfo(){
        $route = 'https://channel-api.lianlianlvyou.com/ll/channel/order/getOrderInfo';
        $params = json_encode([
            'channelId'=>Config::get('lian.channelId'),
            'thirdOrderId'=>'123456789',
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    public function generateQrCode(){
        $route = 'https://channel-api.lianlianlvyou.com/channel/order/generateQrCode';
        $params = json_encode([
            'channelId'=>Config::get('lian.channelId'),
            'channelOrderId'=>'847881841',
        ]);
//        dump($params);die;
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    /**
     * 订单退款
     */
    public function refund($channelOrderNo,$orderNoList){
        $route = 'https://channel-api.lianlianlvyou.com/channel/order/refund';
        $params = json_encode([
            'channelOrderNo'=>$channelOrderNo,
            'orderNoList'=>[$orderNoList],
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }
    /**
     * 生成订单
     */
    public function creates($outOrderId,$validToken,$productId,$itemId,$settlePrice,$customerName,$customerPhoneNumber,$num,$thirdSalePrice,$idCard,$travelDate){
        $route = 'https://channel-api.lianlianlvyou.com/channel/order/create';
        $params = json_encode([
            'validToken'=>$validToken,
            'thirdPartyOrderNo'=>$outOrderId,
            'productId'=>$productId,
            'itemId'=>$itemId,
            'settlePrice'=>(int)$settlePrice,
            'customerName'=>$customerName,
            'customerPhoneNumber'=>$customerPhoneNumber,
            'payType'=>1,
            'quantity'=>$num,
            'purchaseTime'=>date('Y-m-d H:i:s'),
            'thirdSalePrice'=>(int)$thirdSalePrice, //我们销售价
            'idCard'=>$idCard,
            'travelDate'=>$travelDate,
//            'memo'=>'备注',
            'locationId'=>1
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    /**
     * 检查订单
     */
    public function valid($thirdPartyOrderNo,$productId,$itemId,$settlePrice,$customerName,$customerPhoneNumber,$quantity,$thirdSalePrice,$idCard,$travelDate){
        $route = 'https://channel-api.lianlianlvyou.com/channel/order/valid';
        $params = json_encode([
            'thirdPartyOrderNo'=>$thirdPartyOrderNo,
            'productId'=>$productId,
            'itemId'=>$itemId,
            'settlePrice'=>(int)$settlePrice,
            'customerName'=>$customerName,
            'customerPhoneNumber'=>$customerPhoneNumber,
            'payType'=>1,
            'quantity'=>$quantity,
            'purchaseTime'=>date('Y-m-d H:i:s'),
            'thirdSalePrice'=>(int)$thirdSalePrice,
            'idCard'=>$idCard,
            'travelDate'=>$travelDate,
            'locationId'=>1
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    /**
     * 获取产品详情html
     */
    public function getProductV1Html(){
        $route = 'https://channel-api.lianlianlvyou.com/ll/channel/product/getProductV1Html';
        $params = json_encode([
            'productId'=>'3350971',
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }
    /**
     * 获取产品详情html
     */
    public function getProductHtml($productId,$itemIdList){
        $route = 'https://channel-api.lianlianlvyou.com/ll/channel/product/getProductHtml';
        $params = json_encode([
            'productId'=>$productId,
            'itemIdList'=>[$itemIdList]
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    /**
     * 获取产品详情
     */
    public function getProductDetails($item_id,$itemIdList=''){
        $route = 'https://channel-api.lianlianlvyou.com/ll/channel/product/getProductDetails';
        $params = json_encode([
            'channelId'=>Config::get('lian.channelId'),
            'productId'=>$item_id,
            'itemIdList'=>[$itemIdList]
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    /**
     * 获取产品列表
     */
    public function getProductList($cityCode,$pageNum,$pageSize){
        $route = 'https://channel-api.lianlianlvyou.com/ll/channel/product/getProductList';
        $params = json_encode([
            'channelId'=>Config::get('lian.channelId'),
            'cityCode'=>$cityCode,
            'pageNum'=>$pageNum,
            'pageSize'=>$pageSize,
        ]);
        $params = self::encrypt($params);
        $res = self::send($route,$params);
        return $res;
    }

    /**
     * 公共方法
     */
    static private function send($route,$params){
        $paramArr['encryptedData'] = $params;
        $paramArr['timestamp'] =intval(microtime(true)*1000);
        $paramArr['channelId'] = Config::get('lian.channelId');
        $paramArr['sign'] = md5($params. $paramArr['channelId'].$paramArr['timestamp']);
        $paramArr = json_encode($paramArr);
        $paramArrs[] =  'Content-Type:application/json';
        $result =self::curl($route,$paramArr);
//        Db::name('ll_codeorder')->insert(['info'=>$result]);
        $result = json_decode($result,true);
        if($result['success'] == true){
            $encryptedData = self::decrypt($result['data']['encryptedData']);
            $encryptedData = json_decode($encryptedData,true);
            $result['data']['encryptedData'] = $encryptedData;
        }
        return  $result;
    }

    /**
     * 加密
     */
    public static function encrypt($data)
    {
        $key = Config::get('lian.Key');
        $data =  openssl_encrypt($data, 'AES-128-ECB', base64_decode($key), OPENSSL_RAW_DATA);
        return base64_encode($data);
    }

    /**
     * 解密
     */
    public static function decrypt($data)
    {
        $key = Config::get('lian.Key');
        $encrypted = base64_decode($data);
        return openssl_decrypt($encrypted, 'AES-128-ECB',base64_decode($key), OPENSSL_RAW_DATA);
    }
    //请求
    public static function curl($url, $postFields = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt ($ch, CURLOPT_USERAGENT, "top-sdk-php");
        // https 请求
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https")
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $header = array("content-type: application/json; charset=UTF-8");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        $reponse = curl_exec($ch);
        curl_close($ch);
        return $reponse;
    }
}